package com.icode.core.shared;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-2-2
 * Time: 上午11:40
 * To change this template use File | Settings | File Templates.
 */
public class MailHandle {

    /**
     * 邮件：普通格式
     */
    public static void sendMail() {
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
        // 设定mail server
        senderImpl.setHost("smtp.qq.com");

        // 建立邮件消息
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        // 设置收件人，寄件人 用数组发送多个邮件
        // String[] array = new String[]    {"sun111@163.com","sun222@sohu.com"};
        // mailMessage.setTo(array);
        mailMessage.setTo("418791536@qq.com");
        mailMessage.setFrom("1411802214@qq.com");
        mailMessage.setSubject("测试标题！");
        mailMessage.setText("测试内容！！");

        senderImpl.setUsername("1411802214@qq.com");  //  根据自己的情况,设置username
        senderImpl.setPassword("liufei1536");  //  根据自己的情况, 设置password

        Properties prop = new Properties();
        prop.put(" mail.smtp.auth ", "true");  //  将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
        prop.put(" mail.smtp.timeout ", " 25000 ");
        senderImpl.setJavaMailProperties(prop);
        // 发送邮件
        senderImpl.send(mailMessage);

        System.out.println(" 邮件发送成功.. ");
    }

    /**
     * 邮件：HTML格式
     *
     * @throws MessagingException
     */
    public static void sendHTMLEmail(String balance) throws MessagingException {
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

        //设定mail server
        senderImpl.setHost("smtp.qq.com");

        //建立邮件消息,发送简单邮件和html邮件的区别
        MimeMessage mailMessage = senderImpl.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, "utf-8");

        //设置收件人，寄件人
        String[] array = {"418791536@qq.com"};
        messageHelper.setTo(array);
        messageHelper.setFrom("1411802214@qq.com");
        messageHelper.setSubject("漫庭网络技术有限公司");
        //true 表示启动HTML格式的邮件
        messageHelper.setText("<html><head></head><body><h3>尊敬的客服：</h3><br/><p style='margin-left:40px;'>华兴软通短信服务的短信余额已不足200条，请尽快充值!</p><br/><p style='margin-left:40px;'>当前余额为：" + balance + "</p></body></html>", true);

        senderImpl.setUsername("1411802214@qq.com"); // 根据自己的情况,设置username
        senderImpl.setPassword("liufei1536"); // 根据自己的情况, 设置password
        Properties prop = new Properties();
        prop.put("mail.smtp.auth", "true"); // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
        prop.put("mail.smtp.timeout", "25000");
        senderImpl.setJavaMailProperties(prop);
        //发送邮件
        senderImpl.send(mailMessage);
    }

    /**
     * 邮件：带图片，非附件
     *
     * @throws MessagingException
     */
    public static void sendMail3() throws MessagingException {
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

        //设定mail server
        senderImpl.setHost("smtp.qq.com");

        //建立邮件消息,发送简单邮件和html邮件的区别
        MimeMessage mailMessage = senderImpl.createMimeMessage();
        //注意这里的boolean,等于真的时候才能嵌套图片，在构建MimeMessageHelper时候，所给定的值是true表示启用，
        //multipart模式
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);

        //设置收件人，寄件人
        messageHelper.setTo("418791536@qq.com");
        messageHelper.setFrom("1411802214@qq.com");
        messageHelper.setSubject("测试邮件中嵌套图片!！");
        //true 表示启动HTML格式的邮件
        messageHelper.setText("<html><head></head><body><h1>hello!!spring image html mail</h1>" +
                "<img src=/\"cid:aaa/\"/></body></html>", true);

        FileSystemResource img = new FileSystemResource(new File("C:/Users/xianfei/Desktop/testmt/1.png"));
        messageHelper.addInline("aaa", img);

        senderImpl.setUsername("1411802214@qq.com"); // 根据自己的情况,设置username
        senderImpl.setPassword("liufei1536"); // 根据自己的情况, 设置password
        Properties prop = new Properties();
        prop.put("mail.smtp.auth", "true"); // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
        prop.put("mail.smtp.timeout", "25000");
        senderImpl.setJavaMailProperties(prop);

        //发送邮件
        senderImpl.send(mailMessage);

        System.out.println("邮件发送成功..");
    }

    /**
     * 邮件：带附件
     *
     * @throws MessagingException
     */
    public static void sendMail4() throws MessagingException {
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

        //设定mail server
        senderImpl.setHost("smtp.qq.com");
        //建立邮件消息,发送简单邮件和html邮件的区别
        MimeMessage mailMessage = senderImpl.createMimeMessage();
        //注意这里的boolean,等于真的时候才能嵌套图片，在构建MimeMessageHelper时候，所给定的值是true表示启用，
        //multipart模式 为true时发送附件 可以设置html格式
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");

        //设置收件人，寄件人
        messageHelper.setTo("418791536@qq.com");
        messageHelper.setFrom("1411802214@qq.com");
        messageHelper.setSubject("测试邮件中上传附件!！");

        //true 表示启动HTML格式的邮件
        messageHelper.setText("<html><head></head><body><h1>你好：附件中有学习资料！</h1></body></html>", true);
        FileSystemResource file = new FileSystemResource(new File("C:/Users/xianfei/Desktop/MySQL中文参考手册.chm"));
        try {
            //这里的方法调用和插入图片是不同的。
            messageHelper.addAttachment(MimeUtility.encodeWord("MySQL中文参考手册.chm"), file);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        senderImpl.setUsername("1411802214@qq.com"); // 根据自己的情况,设置username
        senderImpl.setPassword("liufei1536"); // 根据自己的情况, 设置password
        Properties prop = new Properties();
        prop.put("mail.smtp.auth", "true"); // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
        prop.put("mail.smtp.timeout", "25000");
        senderImpl.setJavaMailProperties(prop);
        //发送邮件
        senderImpl.send(mailMessage);

        System.out.println("邮件发送成功..");
    }

}
